package edu.uic.keyoutcomestrackertool.fragments;

import java.util.List;

import edu.uic.keyoutcomestrackertool.R;
import edu.uic.keyoutcomestrackertool.TrackKeyOutcomes;
import edu.uic.kott.db.KottDataSource;
import edu.uic.kott.model.Course;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TextView;

public class TrackStudySessions extends Fragment {
	
	static TextView classStudySessionsOn;
	public static String studySessionsDate;
	
	private TableLayout studySessionsQueryLayout;
	
	KottDataSource datasource;
	List<Course> studyList;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View track_study_sessions_view = inflater.inflate(R.layout.track_study_sessions, container, false);
		
		classStudySessionsOn = (TextView) track_study_sessions_view.findViewById(R.id.classStudySessionsOn);
		classStudySessionsOn.setText(TrackKeyOutcomes.activeDate);
		
		studySessionsQueryLayout = (TableLayout) track_study_sessions_view.findViewById(R.id.studySessionsQueryLayout);
		
		TrackKeyOutcomes.studySessionsVisible = true;
		TrackKeyOutcomes.UpdateVariables();
		
		StudySessionsData();
		
		return track_study_sessions_view;
	}
	
	
	public void StudySessionsData() {
		datasource = new KottDataSource(this.getActivity());
		
		/*for (Course c : query) {
			
			long courseId = c.getCourseId();
			String courseName = c.getCourseName();
			String targetedWSS = c.getTargetedWSS();
			int index = (int) courseId;
			
			makeStudySessionsGUI(courseId, courseName, targetedWSS, index);
		}*/
		
		//Above should end up looping like this
		makeStudySessionsGUI(0, "Math", "6", 0);
		makeStudySessionsGUI(1, "Science", "6", 1);
		
	}


	public static void StudySessionsUpdateOnDateChange() {
		studySessionsDate = TrackKeyOutcomes.activeDate;
		classStudySessionsOn.setText("on: " + studySessionsDate);
	}

	@Override
	public void onStop() {
		TrackKeyOutcomes.studySessionsVisible = false;
		super.onStop();
	}
	
	@Override
	public void onResume() {
		TrackKeyOutcomes.studySessionsVisible = true;
		super.onResume();
	}
	
	public void makeStudySessionsGUI(long classId, String className, String targetedWSS, int index) {
		LayoutInflater inflater = (LayoutInflater) this.getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	
		View newRowView = inflater.inflate(R.layout.study_sessions_group_view, null);
		
		TextView classText = (TextView) newRowView.findViewById(R.id.studySessionsClassName);
		classText.setText(className);
		
		TextView targetText = (TextView) newRowView.findViewById(R.id.studySessionsTargeted);
		targetText.setText(getResources().getString(R.string.targetedWeeklyStudySessions) + ": " + targetedWSS);
		
		EditText completedEditText = (EditText) newRowView.findViewById(R.id.studySessionsCompleted);
		completedEditText.setOnKeyListener(completedKeyListener);
		
		TextView weekToDate = (TextView) newRowView.findViewById(R.id.studySessionsWeekToDate);
		weekToDate.setText(getResources().getString(R.string.weekToDateCompletedStudySessions) + " " + String.valueOf(WeekToDateSS(classId)));
	
		TextView termToDate = (TextView) newRowView.findViewById(R.id.studySessionsTermToDate);
		termToDate.setText(getResources().getString(R.string.termToDateCompletedStudySessions) + " " + String.valueOf(TermToDateSS(classId)));
	
		studySessionsQueryLayout.addView(newRowView, index);
	}
	
	private long TermToDateSS(long classId) {
	
		int targeted = 9;
		int completed = 9;
		
		long percentage = completed/targeted;
		
		return percentage;
	}


	private long WeekToDateSS(long classId) {
	
		int targeted = 9;
		int completed = 9;
		
		long percentage = completed/targeted;
		
		return percentage;
	}

	public OnKeyListener completedKeyListener = new OnKeyListener() {

		@Override
		public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
			// TODO Auto-generated method stub
			return false;
		}
		
	};
}
